package com.alibobo.tech5.sixenine;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import android.util.Log;

import com.alibobo.tech5.ganji.GJR;
import com.alibobo.tech5.sixenine.internal.PanEntry;
import com.alibobo.tech5.sixenine.internal.PanKey;

@SuppressWarnings("unchecked")
@Deprecated
public class SixNinePan {

	Hashtable<String, Object> _content = new Hashtable<String, Object>();

	public Hashtable<String, Object> getContent() {
		return _content;
	}

	protected List<String> cal四課(String 日干, String 日支, int offset) {

		String 寄宮 = GJR.get天干寄宮(日干);
		String 干上 = GJR.offset(寄宮, offset);
		String 干上上 = GJR.offset(干上, offset);
		String 支上 = GJR.offset(日支, offset);
		String 支上上 = GJR.offset(支上, offset);
		List<String> 四課 = new ArrayList<String>();
		四課.add(干上 + 日干);
		四課.add(干上上 + 干上);
		四課.add(支上 + 日支);
		四課.add(支上上 + 支上);
		return 四課;
	}

	protected void prepare四課() {
		List<String> clazz4 = cal四課(get日干(), get日支(), getOffset());
		_content.put("四課", clazz4);
	}

	protected void prepare三傳() {
		String ss = (String) _content.get(PanKey.三傳);
		String 日干 = get日干();
		int offset = getOffset();

		String[] tname = { "初傳", "中傳", "末傳" };
		Hashtable<String, String> 遁干表 = get遁干表();

		for (int i = 0; i < 3; i++) {
			try {
				Hashtable<String, String> tran = new Hashtable<String, String>();
				// 這是該死的subString..因為第二個參數是end
				String s = String.valueOf(ss.charAt(i));
				tran.put("支", s);
				tran.put("地 ", GJR.offset(s, -offset));
				tran.put("陰神 ", GJR.offset(s, offset));
				tran.put("遁干", 遁干表.get(s));
				tran.put("六親", GJR.六親AofB(s, 日干));
				_content.put(tname[i], tran);
			} catch (Exception e) {
				e.printStackTrace();
			}
			Log.i("c", "");
		}
	}

	protected static String Key_遁干表 = "遁干表";

	protected Hashtable<String, String> get遁干表() {
		Hashtable<String, String> t = (Hashtable<String, String>) _content
				.get(Key_遁干表);
		if (t == null) {
			t = GJR.get遁干表(get日干支());
			_content.put(Key_遁干表, t);
		}
		return t;
	}

	protected String get旬首() {
		return GJR.get旬首(get日干支());
	}

	protected void prepare天地盤() {
		int offset = getOffset();
		int offset2 = offset * 2;
		Hashtable<String, Hashtable<String, String>> pan = new Hashtable<String, Hashtable<String, String>>();
		for (int id = 1; id <= 12; id++) {
			String ji = GJR.id2Ji(id);
			PanEntry pe = new PanEntry(new Hashtable<String, String>());
			pe.set地盤(ji);
			pe.set天盤(GJR.offset(ji, offset));
			pe.set陰神(GJR.offset(ji, offset2));
			pe.set支神(GJR.get支神(pe.get天盤()));
			pan.put(ji, pe.getContent());
			pe.releaseBind();
		}
		_content.put("盤", pan);
	}

	protected Hashtable<String, Hashtable<String, String>> get盤() {

		Hashtable<String, Hashtable<String, String>> pan = (Hashtable<String, Hashtable<String, String>>) _content
				.get("盤");
		return pan;
	}

	public PanEntry get盤位by天盤(String 天盤) {
		Hashtable<String, Hashtable<String, String>> ps = get盤();
		for (Hashtable<String, String> ct : ps.values()) {
			if (天盤.equals(ct.get("天盤"))) {
				PanEntry pe = new PanEntry(ct);
				return pe;
			}
		}
		return null;
	}

	public PanEntry get盤位by地盤(String 地盤) {
		Hashtable<String, String> _ct = (Hashtable<String, String>) get盤().get(
				地盤);
		PanEntry pe = new PanEntry(_ct);
		return pe;
	}

	public PanEntry get天將位(String 天將) {

		Hashtable<String, Hashtable<String, String>> ps = get盤();
		for (Hashtable<String, String> ct : ps.values()) {
			if (天將.equals(ct.get("天將"))) {
				PanEntry pe = new PanEntry(ct);
				return pe;
			}
		}
		return null;

	}

	public SixNinePan() {

	}

	public SixNinePan(Hashtable<String, Object> c) {
		setShortPan(c);
	}

	public void setShortPan(Hashtable<String, Object> c) {
		for (String k : c.keySet()) {
			_content.put(k, c.get(k));
		}
		prepare天地盤();
		prepare四課();
		prepare三傳();
	}

	public int getOffset() {
		return (Integer) _content.get(PanKey.panOffset);
	}

	public void set時干支(String value) {
		_content.put("時干支", value);
	}

	public String get時干支() {
		return (String) _content.get("時干支");
	}

	public String get時干() {
		return GJR.pickFirstWord((String) _content.get("時干支"));
	}

	public String get時支() {
		return GJR.pickLastWord((String) _content.get("時干支"));
	}

	public String get日干支() {
		return (String) _content.get(PanKey.日干支);
	}

	public String get日干() {
		String gj = get日干支();
		return GJR.pickFirstWord(gj);
	}

	public String get日支() {
		String gj = get日干支();
		return GJR.pickLastWord(gj);
	}

	public String get三傳() {
		String ss = (String) _content.get(PanKey.三傳);
		return ss;
	}

	public Hashtable<String, String> get初傳() {
		return (Hashtable<String, String>) _content.get("初傳");
	}

	public Hashtable<String, String> get中傳() {
		return (Hashtable<String, String>) _content.get("中傳");
	}

	public Hashtable<String, String> get末傳() {
		return (Hashtable<String, String>) _content.get("末傳");
	}

	public List<String> get日貴() {
		return (List<String>) _content.get(PanKey.日貴);
	}

	public List<String> get夜貴() {
		return (List<String>) _content.get(PanKey.夜貴);
	}

	public void update課傳之天將() {
		String[] tname = { "初傳", "中傳", "末傳" };
		for (int i = 0; i < 3; i++) {
			Hashtable<String, String> tran = (Hashtable<String, String>) _content
					.get(tname[i]);
			String ji = tran.get("支");
			String 天將 = get盤位by天盤(ji).get天將();
			tran.put("天將", 天將);
		}
	}

	public void set月將(String m) {
		_content.put("月將", m);
	}

	public String get月將() {
		return getNotNullString("月將");
	}

	public void set月干支(String m) {
		_content.put("月干支", m);
	}

	public String get月干支() {
		return getNotNullString("月干支");
	}

	public void set年干支(String m) {
		_content.put("年干支", m);
	}

	public String get年干支() {
		return getNotNullString("年干支");
	}

	protected String getNotNullString(String key) {
		String r = (String) _content.get(key);
		return r == null ? "" : r;
	}

}
